<?php

namespace App\Http\Controllers;

use App\Exports\LedgerTenderExport;
use App\Exports\StoreroomExport;
use App\Imports\ArchiveManageVolumeImport;
use App\Imports\DocArchiveImport;
use App\Imports\StoreroomImport;
use App\Models\ArchiveManage;
use App\Models\ArchiveManageVolume;
use App\Models\Menu;
use App\Models\Project;
use App\Models\Storeroom;
use App\Services\ArchiveManageService;
use App\Services\CommonService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class ArchiveManageController extends Controller
{
    public $archiveManageService;

    public function __construct(ArchiveManageService $archiveManageService)
    {
        parent::__construct();
        $this->archiveManageService = $archiveManageService;
    }

    /**
     * 获取项目
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/5/9 13:49
     */
    public function getProjects(Request $request): JsonResponse
    {
        $year        = $request->input('year');
        $agency      = $request->input('agency');
        $storePeriod = $request->input('store_period');

        $data = $this->archiveManageService->getProjects($year, $agency, $storePeriod);

        return $this->success($data);
    }

    /**
     * 获取年份
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/5/9 13:49
     */
    public function getYears(Request $request): JsonResponse
    {
        $project     = $request->input('project');
        $agency      = $request->input('agency');
        $storePeriod = $request->input('store_period');

        $data = $this->archiveManageService->getYears($project, $agency, $storePeriod);

        return $this->success($data);
    }

    /**
     * 获取机构
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/5/9 13:52
     */
    public function getAgencies(Request $request): JsonResponse
    {
        $project     = $request->input('project');
        $year        = $request->input('year');
        $storePeriod = $request->input('store_period');

        $data = $this->archiveManageService->getAgencies($project, $year, $storePeriod);

        return $this->success($data);
    }

    /**
     * 获取保管期限
     *
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/5/9 12:02
     */
    public function getStorePeriods(Request $request) : JsonResponse
    {
        $project = $request->input('project');
        $year    = $request->input('year');
        $agency  = $request->input('agency');

        $data = $this->archiveManageService->getStorePeriods($project, $year, $agency);

        return $this->success($data);
    }

    /**
     * 导入文书档案
     *
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Exceptions\ApiException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @author: lxk
     * @date: 2024-0-08 9:02
     */
    public function importDocArchive(Request $request): JsonResponse
    {
        CommonService::isFileValid($request); //验证文件是否合法
        $importCountKey = get_rand_str('import_doc_archive_count');
        redis()->setex($importCountKey, 600, 0); //用时最大10分钟
        Excel::import(new DocArchiveImport($importCountKey), $request->file('file'));
        $count = redis()->get($importCountKey) ?: 0;

        CommonService::addActionLog("导入文书档案", "【{$count}】条数据");

        return $this->success('', "成功导入【{$count}】条数据");
    }


    /**
     * 获取档案类型 立卷说明、招投标、文书档案等
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024-04-22 9:01
     */
    public function getDocArchives(Request $request): JsonResponse
    {
        $project     = $request->input('project');
        $year        = $request->input('year');
        $agency      = $request->input('agency');
        $storePeriod = $request->input('store_period');
        $keyword     = $request->input('keyword');
        $perPage     = $request->input('per_page');

        $data = $this->archiveManageService->getDocArchives($project, $year, $agency, $storePeriod, $keyword, $perPage);

        return $this->success($data);
    }

    /**
     * 导出数据
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024-12-29 9:07
     */
    public function export(Request $request): JsonResponse
    {
        $project     = $request->input('project');
        $year        = $request->input('year');
        $agency      = $request->input('agency');
        $storePeriod = $request->input('store_period');
        $keyword     = $request->input('keyword');

        $data = $this->archiveManageService->export($project, $year, $agency, $storePeriod, $keyword);

        return $this->success($data);
    }


    /**
     * 更新数据
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/5/9 16:22
     */
    public function update(Request $request): JsonResponse
    {
        $params = $request->input();
        if ($this->archiveManageService->updateData($params)) {
            CommonService::addActionLog("修改文书档案，数据ID={$params['id']}");
            return $this->success('', '保存成功！');
        } else {
            return $this->error('保存失败！');
        }
    }

    /**
     * 清空数据
     *
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/5/9 14:59
     */
    public function clearData(): JsonResponse
    {
        $companyId       = company_id();
        $docArchiveTable = "doc_archives_{$companyId}";
        DB::table($docArchiveTable)->truncate();

        CommonService::addActionLog("清空文书档案");

        return $this->success('', '数据已清空！');
    }


    /**
     * 获取档案类型 立卷说明、招投标、文书档案等
     *
     * @author: lxk
     * @date: 2024/4/22 10:23
     */
    public function getTypes(): JsonResponse
    {
        $types = Menu::query()->where('parent_id', 61)->get()->toArray();

        return $this->success($types);
    }

    /**
     * 保存数据，立卷说明、业务规范、管理制度
     *
     * @author: lxk
     * @date: 2024/4/23 14:34
     */
    public function store(Request $request): JsonResponse
    {
        $id      = $request->input('id');
        $content = $request->input('content');

        ArchiveManage::query()->where('id', $id)->update(['content' => $content]);

        return $this->success('', '已保存！');
    }

    /**
     * 获取内容 立卷说明、业务规范、管理制度
     *
     * @author: lxk
     * @date: 2024/4/23 14:34
     */
    public function getContent(Request $request): JsonResponse
    {
        $id = $request->input('id');

        $info = ArchiveManage::query()->where('id', $id)->first();

        return $this->success($info);
    }

}
